package allod.gui

import javax.swing._
import scala.swing.event.ButtonClicked
import swing._
import scala.collection.mutable
import allod.som.{Tuner, Lattice, Metric}
import allod.som.functions.step.ExponentialStepFunction
import allod.som.functions.topological.Gaussian
import allod.som.initializer.{RandomInitializer, SampleInitializer}
import allod.som.functions.metric.EuclideanMetric
import allod.som.input.InputSource

/**
 * Created by IntelliJ IDEA.
 * User: allod
 * Date: 5 квіт 2010
 * Time: 22:01:16
 */

class SettingsFrame(owner: JFrame, modal: Boolean) extends JDialog(owner, modal) {
  setTitle("SOM Settings")

  private var profit: Boolean = false

  def isApprovalDecision: Boolean = this.profit

  val rootPanel = new MigPanel("insets 0, wrap 2", "[fill,grow][fill,grow]",
    "[][][][][][][]push[pref!]")
  this.setContentPane(rootPanel.peer)

  rootPanel.add(new Label("Columns:"))
  val columnsSpinner = new JSpinner(new SpinnerNumberModel(10, 1, 256, 1))
  rootPanel.add(columnsSpinner)

  rootPanel.add(new Label("Rows:"))
  val rowsSpinner = new JSpinner(new SpinnerNumberModel(10, 1, 256, 1))
  rootPanel.add(rowsSpinner)

  val topologyPanel = new MigPanel("wrap 2", "[fill,grow][fill,grow]")
  topologyPanel.border = BorderFactory.createTitledBorder("Topological neighbourhood")

  topologyPanel.add(new Label("Neighbourhood function type:"))
  val topologyTypeComboBox = new ComboBox(List("Gausian"));
  topologyPanel.add(topologyTypeComboBox)

  topologyPanel.add(new Label("Effective width \u03c3\u2080 ="))
  val sigma0Field = new TextField("3");
  topologyPanel.add(sigma0Field)

  topologyPanel.add(new Label("Constant \u03c4\u2081 ="))
  val tau1Field = new TextField("2000");
  topologyPanel.add(tau1Field)
  rootPanel.add(topologyPanel, "span")

  val stepPanel = new MigPanel("wrap 2", "[fill,grow][fill,grow]")
  stepPanel.border = BorderFactory.createTitledBorder("Step function")

  stepPanel += new Label("Type:")
  val stepTypeComboBox = new ComboBox(List("Exponential"))
  stepPanel += stepTypeComboBox

  stepPanel += new Label("Constant \u03b7\u2080 =")
  val eta0Field = new TextField("0.1")
  stepPanel += eta0Field

  stepPanel += new Label("Constant \u03c4\u2082 =")
  val tau2Field = new TextField("10000")
  stepPanel += tau2Field
  rootPanel.add(stepPanel, "span")

  rootPanel += new Label("Initializer:")
  val initializerComboBox = new ComboBox(List("Sample"))
  rootPanel += initializerComboBox

  rootPanel += new Label("Iterations count:")
  val iterationsSpinner = new JSpinner(new SpinnerNumberModel(50000, 1000, 256000000, 100))
  rootPanel.add(iterationsSpinner)

  rootPanel += new Label("Number of frames in training set:")
  val framesCount = new JSpinner(new SpinnerNumberModel(1, 1, 1, 1))
  framesCount.setEnabled(false)
  rootPanel += framesCount

  val buttonsPanel = new MigPanel("insets", "[fill,grow][fill,grow]")
  val createButton = new Button("Create")
  createButton.reactions += {
    case ButtonClicked(_) =>
      profit = true
      SettingsFrame.this.setVisible(false)
  }
  buttonsPanel += createButton

  val cancelButton = new Button("Cancel")
  cancelButton.reactions += {
    case ButtonClicked(_) => profit = false
    SettingsFrame.this.setVisible(false)
  }
  buttonsPanel += cancelButton
  rootPanel.add(buttonsPanel, "span")

  this.pack()
  this.setLocationRelativeTo(null)

  def getSettings(): Map[String, Double] = {
    val settings = mutable.Map.empty[String, Double]
    settings += "columns" -> columnsSpinner.getValue.toString.toDouble
    settings += "rows" -> rowsSpinner.getValue.toString.toDouble
    settings += "topology" -> topologyTypeComboBox.selection.index.toDouble
    settings += "sigma0" -> sigma0Field.text.toDouble
    settings += "tau1" -> tau1Field.text.toDouble
    settings += "stepType" -> stepTypeComboBox.selection.index.asInstanceOf[Double]
    settings += "eta0" -> eta0Field.text.toDouble
    settings += "tau2" -> tau2Field.text.toDouble
    settings += "initializer" -> initializerComboBox.selection.index.asInstanceOf[Double]
    settings += "iterations" -> iterationsSpinner.getValue.toString.toDouble
    Map.empty[String, Double] ++ settings
  }
}